# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=newlib
pkgver=4.3.0.20230120
pkgrel=3
pkgdesc="C standard library implementation intended for use on embedded systems"
url="https://www.sourceware.org/newlib/"
makedepends="texinfo"
arch="aarch64 ppc64le x86_64 x86"
license="GPL-2.0-only AND LGPL-2.1-only AND 0BSD AND BSD-3-Clause AND GPL-3.0-only AND LGPL-3.0-only"
source="
	https://sourceware.org/pub/newlib/newlib-$pkgver.tar.gz

	msp430-elf-nano.specs
	0001-msp430-enable-syscalls.patch
	"
options="!check"

# classify targets as exotic or mainstream. Only build support for
# exotic targets on typical developer / CI hardware (x86_86, aarach64).
_targets_mainstream="
	arm-none-eabi
	riscv-none-elf
	"

_targets_exotic="
	msp430-elf
	or1k-elf
	aarch64-none-elf
	"

case "$CARCH" in
x86_64|aarch64)
	# support all targets on typical workstation / notebook / CI servers
	_targets="$_targets_mainstream $_targets_exotic"
	;;
*)
	# support only mainstream targets on exotic hosts
	_targets="$_targets_mainstream"
	;;
esac

for target in $_targets; do
	targetnorm="${target//-/_}"
	subpackages="newlib-$target:$targetnorm $subpackages"
	# here only makedepends. The depends to binutils-$target and gcc-$target
	# will be set in the subpkg newlib-$target
	makedepends="binutils-$target gcc-$target $makedepends"
done

# secfixes:
#   4.1.0-r0:
#     - CVE-2021-3420

# This package installs two version of the newlib library to remain compatible
# with projects expecting the GNU ARM Embedded Toolchain [1]. The nano version
# appends the postfix "_nano" to the libraries. The is not an arbitrary choice,
# it matches the specs in "/usr/arm-none-eabi/lib/nano.specs", which is
# part of the default newlib installation.
# [1]: https://launchpad.net/gcc-arm-embedded/

build() {
	for target in $_targets; do
		mkdir "$builddir/build-$target" "$builddir/build-$target-nano"
		# nano version
		cd "$builddir/build-$target-nano"
		export CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections -fshort-wchar"
		"$builddir"/configure \
				--target=$target \
				--prefix=/usr/ \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--disable-newlib-fseek-optimization \
				--disable-newlib-fvwrite-in-streamio \
				--disable-newlib-supplied-syscalls \
				--disable-newlib-unbuf-stream-opt \
				--disable-newlib-wide-orient \
				--disable-nls \
				--enable-lite-exit \
				--enable-newlib-global-atexit \
				--enable-newlib-nano-formatted-io \
				--enable-newlib-nano-malloc \
				--enable-newlib-reent-small \
				--enable-newlib-retargetable-locking \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make

		# regular version
		cd "$builddir/build-$target"
		export CFLAGS_FOR_TARGET="-O2 -ffunction-sections -fdata-sections"
		"$builddir"/configure \
				--host=$CHOST \
				--target=$target \
				--prefix=/usr \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--disable-newlib-supplied-syscalls \
				--disable-nls \
				--enable-newlib-io-c99-formats \
				--enable-newlib-io-long-long \
				--enable-newlib-register-fini \
				--enable-newlib-retargetable-locking \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make
	done
}

package() {
	for target in $_targets; do
		depends="$depends newlib-$target"
	done
	mkdir -p "$pkgdir"
}

_install_subpkg() {
	target="${subpkgname#newlib-}"
	pkgdesc="A C standard library implementation intended for $target"
	depends="binutils-$target gcc-$target $depends"
	mkdir -p "$subpkgdir"
	# install nano version
	cd "$builddir/build-$target-nano"
	make -j1 DESTDIR="$subpkgdir" install
	# rename nano version libs
	cd "$subpkgdir/usr/$target/lib"
	find . -regex ".*\(c\|g\|m\|rdimon\|gloss\)\.a" | while read -r _file; do
		mv "$_file" "${_file%%.a}_nano.a"
	done
	mkdir -p "$subpkgdir/usr/$target/include/newlib-nano"
	mv "$subpkgdir/usr/$target/include/newlib.h" \
	   "$subpkgdir/usr/$target/include/newlib-nano/"

	# install regular version
	cd "$builddir/build-$target"
	make -j1 DESTDIR="$subpkgdir" install
	rm -rf "$subpkgdir/deleteme"

	# install custom nano specs, if existing
	if [ -e "$srcdir/$target-nano.specs" ]; then
		install -m 644 "$srcdir/$target-nano.specs" \
			"$subpkgdir/usr/$target/lib/nano.specs"
	fi
}

arm_none_eabi() {
	_install_subpkg
}

mips_mti_elf() {
	_install_subpkg
}

msp430_elf() {
	_install_subpkg
}

or1k_elf() {
	_install_subpkg
}

riscv_none_elf() {
	_install_subpkg
}

aarch64_none_elf() {
	_install_subpkg
}

sha512sums="
4a06309d36c2255fef8fc8f2d133cafa850f1ed2eddfb27b5d45f5d16af69e0fca829a0b4c9b34af4ed3a28c6fcc929761e0ee823a4229f35c2853d432b5e7ef  newlib-4.3.0.20230120.tar.gz
647b79e3cc6f3bb98b1263ebbf4b3efa4a4692fddc0df6491c726de2fa0ee46b05ab2ddf96c2c8c785f3cd6a442135ec97bf8df93a8f371d479ea46d03aa8a08  msp430-elf-nano.specs
268c44f8056067e03ee9914d04e8cfa2bb22764bb294b7d16dbfdd51c2e5ef0ad221037a86caafd20ab755b16b38519d61e27ffcd13239cbdbbfad421fa8cb60  0001-msp430-enable-syscalls.patch
"
